/**
 * 
 */
package com.apache.portal.weixin.core.common;

import java.util.HashMap;
import java.util.Map;

import com.apache.portal.common.util.WeiXinTools;
import com.apache.uct.common.ToolsUtil;

/**
  * description: 加载业务系统实现
  * @author Hou DaYu 创建时间：下午02:51:02
 */
public class LoadService {

	private static LoadService instance;

	private Map<String, BaseService> beans = new HashMap<String, BaseService>();
	private Map<String, String> msgKey = new HashMap<String, String>();
	private WeiXinTools tools;

	private LoadService() {
		msgKey.put("service_text", "0");
		msgKey.put("service_news", "0");
		msgKey.put("service_pic", "0");
		msgKey.put("service_orther", "0");
		msgKey.put("service_event", "0");
		tools = new WeiXinTools();
	}

	public static synchronized LoadService getInstance() {
		if (null == instance) {
			instance = new LoadService();
		}
		return instance;
	}

	public void init() {
		for (String key : msgKey.keySet()) {
			if ("0".equals(msgKey.get(key))) {
				String classurl = ToolsUtil.getInstance().getValueByKey(key).trim().toString();
				try {
					Class<?> clazz = Class.forName(classurl);
					LoadService.getInstance().setBeans(key, (BaseService) clazz.newInstance());
				} catch (Exception e) {
					System.out.println("注入接口" + key + ";className=" + classurl + "失败！");
				}
			}
		}
	}

	public WeiXinTools getTools() {
		return tools;
	}

	public void setTools(WeiXinTools tools) {
		this.tools = tools;
	}

	public BaseService getBeans(String key) {
		return beans.get(key);
	}

	public void setBeans(Map<String, BaseService> beans) {
		this.beans = beans;
	}

	public void setBeans(String key, BaseService value) {
		if (null == beans.get(key))
			beans.put(key, value);
	}
}
